package com.lg.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.product.entity.Brand;
import com.lg.product.entity.SpecParam;
import com.lg.product.entity.Spu;
import com.lg.product.entity.SpuDetail;
import com.lg.product.service.IBrandService;
import com.lg.product.service.ISpecParamService;
import com.lg.product.service.ISpuDetailService;
import com.lg.product.service.ISpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import sun.util.resources.LocaleData;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * spu表，该表描述的是一个抽象性的商品，比如 iphone8 前端控制器
 * </p>
 *
 * @author song
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/spu")
public class SpuController {

    @Autowired
    private ISpuService iSpuService;
    @Autowired
    private ISpuDetailService iSpuDetailService;
    @Autowired
    private IBrandService iBrandService;
    @Autowired
    private ISpecParamService iSpecParamService;

    /**
     *商品 spu 查询
     * @param spu
     * @param page
     * @return
     */
    @RequestMapping("listSpu")
    public IPage<Spu> listSpu(Spu spu, Page page){

        return iSpuService.listSpu(spu,page);
    }

    /**
     * 查询所有 品牌
     * @return
     */
    @RequestMapping("brandList")
    public List<Brand> brandList(){
        List<Brand> list = iBrandService.list();
        return list;
    }

    /**
     * 修改 上架 下架
     * @param id
     * @param saleable
     * @return
     */
    @RequestMapping("updateSaleable")
    public int updateSaleable(Long id,Integer saleable){
        //System.out.println("id==="+id+"saleable==="+saleable);
        iSpuService.updateSaleable(id,saleable);

        return 1;
    }

    /**
     * 修改 删除状态
     * @param id
     * @param valid
     * @return
     */
    @RequestMapping("delevalid")
        public int delevalid(Long id,boolean valid){
        //System.out.println("id==="+id+"saleable==="+saleable);
        Spu spu = new  Spu();
       spu.setId(id);
       spu.setValid(valid);
       iSpuService.saveOrUpdate(spu);

        return 1;
    }

    /**
     * 根据cid 查询
     * @param cid
     * @return
     */
    @RequestMapping("specParamList")
    public List<SpecParam> specParamList(Long cid){
        QueryWrapper<SpecParam> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("cid",cid);
        List<SpecParam> list = iSpecParamService.list(objectQueryWrapper);
        return list;
    }

    /**
     * 根据cid generic 查询
     * @param cid
     * @return
     */
    @RequestMapping("genericList")
    public List<SpecParam> genericList(Long cid,boolean generic){
        QueryWrapper<SpecParam> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("cid",cid);
        objectQueryWrapper.eq("generic",generic);
        List<SpecParam> list = iSpecParamService.list(objectQueryWrapper);
        return list;
    }

    /**
     * spu 回显
     * @param id
     * @return
     */
    @RequestMapping("listId")
    public Spu listId(Long id){
       Spu list = iSpuService.listById(id);
       return list;
    }

    /**
     * SpuDetail 回显
     * @param spuid
     * @return
     */
    @RequestMapping("listDetailId")
    public SpuDetail listDetailId(Long spuid){
        SpuDetail byId = iSpuDetailService.getById(spuid);
        return byId;
    }

    /**
     * Spu 添加 基本信息
     * @param spu
     * @return
     */
    @RequestMapping("addSpu")
    public Spu addSpu(@RequestBody Spu spu){
        Long id = null;
        if(spu!=null){
           LocalDateTime now = LocalDateTime.now();
            spu.setCreateTime(now);
            spu.setLastUpdateTime(now);
            boolean b = iSpuService.save(spu);
             id = spu.getId();
            SpuDetail spuDetail = new SpuDetail();
            spuDetail.setSpuId(spu.getId());

            spuDetail.setGenericSpec("{}");
            spuDetail.setSpecialSpec("{}");
            spuDetail.setPackingList(spu.getPackingList());
            spuDetail.setAfterService(spu.getAfterService());
            System.out.println("==**"+spu);
            System.out.println("spuDetail==**"+spuDetail);
            iSpuDetailService.save(spuDetail);
            return spu;
    }
       return spu;
    }

    /**
     * 添加 tb_spu_detail表的 description
     * @param spuId
     * @param description
     * @return
     */
    @RequestMapping("addDescription")
    public boolean addDescription(Long spuId,String description){
        SpuDetail spuDetail = new SpuDetail();
        spuDetail.setSpuId(spuId);
        spuDetail.setDescription(description);
        boolean b = iSpuDetailService.saveOrUpdate(spuDetail);
        return b;
    }
    /**
     * 添加 tb_spu_detail表的 genericSpec
     * @param spuId
     * @param genericSpec
     * @return
     */
    @RequestMapping("addgenericSpec")
    public boolean addgenericSpec(Long spuId,String genericSpec){
        SpuDetail spuDetail = new SpuDetail();
        spuDetail.setSpuId(spuId);
        spuDetail.setGenericSpec(genericSpec);
        boolean b = iSpuDetailService.saveOrUpdate(spuDetail);
        return b;
    }

    /**
     * 添加 tb_spu_detail表的 specialSpec

     * @param spuDetail
     * @return
     */
    @RequestMapping("addSpecialSpec")
    public boolean addSpecialSpec(@RequestBody SpuDetail spuDetail){
        boolean b = iSpuDetailService.saveOrUpdate(spuDetail);
        return b;
    }

}
